using UnityEngine;

namespace Utils.Extends
{
    /// <summary>
    /// Transform 的扩展方法类
    /// </summary>
    public static class TransformExtends
    {
        /// <summary>
        /// 设置Transform世界坐标的X分量
        /// </summary>
        /// <param name="transform">要修改的Transform对象</param>
        /// <param name="x">新的X坐标值</param>
        /// <returns>修改后的Transform对象（支持链式调用）</returns>
        public static Transform SetPositionX(this Transform transform, float x)
        {
            Vector3 vector = transform.position;
            vector.x = x;
            transform.position = vector;
            return transform;
        }

        /// <summary>
        /// 设置Transform世界坐标的Y分量
        /// </summary>
        /// <param name="transform">要修改的Transform对象</param>
        /// <param name="y">新的Y坐标值</param>
        /// <returns>修改后的Transform对象（支持链式调用）</returns>
        public static Transform SetPositionY(this Transform transform, float y)
        {
            Vector3 vector = transform.position;
            vector.y = y;
            transform.position = vector;
            return transform;
        }

        /// <summary>
        /// 设置Transform世界坐标的Z分量
        /// </summary>
        /// <param name="transform">要修改的Transform对象</param>
        /// <param name="z">新的Z坐标值</param>
        /// <returns>修改后的Transform对象（支持链式调用）</returns>
        public static Transform SetPositionZ(this Transform transform, float z)
        {
            Vector3 vector = transform.position;
            vector.z = z;
            transform.position = vector;
            return transform;
        }

        /// <summary>
        /// 设置Transform局部坐标的X分量
        /// </summary>
        /// <param name="transform">要修改的Transform对象</param>
        /// <param name="x">新的局部X坐标值</param>
        /// <returns>修改后的Transform对象（支持链式调用）</returns>
        public static Transform SetLocalPositionX(this Transform transform, float x)
        {
            Vector3 vector = transform.localPosition;
            vector.x = x;
            transform.localPosition = vector;
            return transform;
        }

        /// <summary>
        /// 设置Transform局部坐标的Y分量
        /// </summary>
        /// <param name="transform">要修改的Transform对象</param>
        /// <param name="y">新的局部Y坐标值</param>
        /// <returns>修改后的Transform对象（支持链式调用）</returns>
        public static Transform SetLocalPositionY(this Transform transform, float y)
        {
            Vector3 vector = transform.localPosition;
            vector.y = y;
            transform.localPosition = vector;
            return transform;
        }

        /// <summary>
        /// 设置Transform局部坐标的Z分量
        /// </summary>
        /// <param name="transform">要修改的Transform对象</param>
        /// <param name="z">新的局部Z坐标值</param>
        /// <returns>修改后的Transform对象（支持链式调用）</returns>
        public static Transform SetLocalPositionZ(this Transform transform, float z)
        {
            Vector3 vector = transform.localPosition;
            vector.z = z;
            transform.localPosition = vector;
            return transform;
        }

        /// <summary>
        /// 设置Transform局部缩放的X分量
        /// </summary>
        /// <param name="transform">要修改的Transform对象</param>
        /// <param name="x">新的X缩放值</param>
        /// <returns>修改后的Transform对象（支持链式调用）</returns>
        public static Transform SetLocalScaleX(this Transform transform, float x)
        {
            Vector3 vector = transform.localScale;
            vector.x = x;
            transform.localScale = vector;
            return transform;
        }

        /// <summary>
        /// 设置Transform局部缩放的Y分量
        /// </summary>
        /// <param name="transform">要修改的Transform对象</param>
        /// <param name="y">新的Y缩放值</param>
        /// <returns>修改后的Transform对象（支持链式调用）</returns>
        public static Transform SetLocalScaleY(this Transform transform, float y)
        {
            Vector3 vector = transform.localScale;
            vector.y = y;
            transform.localScale = vector;
            return transform;
        }

        /// <summary>
        /// 设置Transform局部缩放的Z分量
        /// </summary>
        /// <param name="transform">要修改的Transform对象</param>
        /// <param name="z">新的Z缩放值</param>
        /// <returns>修改后的Transform对象（支持链式调用）</returns>
        public static Transform SetLocalScaleZ(this Transform transform, float z)
        {
            Vector3 vector = transform.localScale;
            vector.z = z;
            transform.localScale = vector;
            return transform;
        }

        /// <summary>
        /// 设置Transform世界旋转欧拉角的X分量
        /// </summary>
        /// <param name="transform">要修改的Transform对象</param>
        /// <param name="x">新的X旋转角度值（欧拉角）</param>
        /// <returns>修改后的Transform对象（支持链式调用）</returns>
        public static Transform SetEulerAnglesX(this Transform transform, float x)
        {
            Vector3 eulerAngles = transform.eulerAngles;
            eulerAngles.x = x;
            transform.eulerAngles = eulerAngles;
            return transform;
        }

        /// <summary>
        /// 设置Transform世界旋转欧拉角的Y分量
        /// </summary>
        /// <param name="transform">要修改的Transform对象</param>
        /// <param name="y">新的Y旋转角度值（欧拉角）</param>
        /// <returns>修改后的Transform对象（支持链式调用）</returns>
        public static Transform SetEulerAnglesY(this Transform transform, float y)
        {
            Vector3 eulerAngles = transform.eulerAngles;
            eulerAngles.y = y;
            transform.eulerAngles = eulerAngles;
            return transform;
        }

        /// <summary>
        /// 设置Transform世界旋转欧拉角的Z分量
        /// </summary>
        /// <param name="transform">要修改的Transform对象</param>
        /// <param name="z">新的Z旋转角度值（欧拉角）</param>
        /// <returns>修改后的Transform对象（支持链式调用）</returns>
        public static Transform SetEulerAnglesZ(this Transform transform, float z)
        {
            Vector3 eulerAngles = transform.eulerAngles;
            eulerAngles.z = z;
            transform.eulerAngles = eulerAngles;
            return transform;
        }

        /// <summary>
        /// 设置Transform局部旋转欧拉角的X分量
        /// </summary>
        /// <param name="transform">要修改的Transform对象</param>
        /// <param name="x">新的局部X旋转角度值（欧拉角）</param>
        /// <returns>修改后的Transform对象（支持链式调用）</returns>
        public static Transform SetLocalEulerAnglesX(this Transform transform, float x)
        {
            Vector3 eulerAngles = transform.localEulerAngles;
            eulerAngles.x = x;
            transform.localEulerAngles = eulerAngles;
            return transform;
        }

        /// <summary>
        /// 设置Transform局部旋转欧拉角的Y分量
        /// </summary>
        /// <param name="transform">要修改的Transform对象</param>
        /// <param name="y">新的局部Y旋转角度值（欧拉角）</param>
        /// <returns>修改后的Transform对象（支持链式调用）</returns>
        public static Transform SetLocalEulerAnglesY(this Transform transform, float y)
        {
            Vector3 eulerAngles = transform.localEulerAngles;
            eulerAngles.y = y;
            transform.localEulerAngles = eulerAngles;
            return transform;
        }

        /// <summary>
        /// 设置Transform局部旋转欧拉角的Z分量
        /// </summary>
        /// <param name="transform">要修改的Transform对象</param>
        /// <param name="z">新的局部Z旋转角度值（欧拉角）</param>
        /// <returns>修改后的Transform对象（支持链式调用）</returns>
        public static Transform SetLocalEulerAnglesZ(this Transform transform, float z)
        {
            Vector3 eulerAngles = transform.localEulerAngles;
            eulerAngles.z = z;
            transform.localEulerAngles = eulerAngles;
            return transform;
        }

        /// <summary>
        /// 设置RectTransform锚定位置的X分量
        /// </summary>
        /// <param name="rectTransform">要修改的RectTransform对象</param>
        /// <param name="x">新的X锚定位置值</param>
        /// <returns>修改后的RectTransform对象（支持链式调用）</returns>
        public static RectTransform SetAnchoredPositionX(this RectTransform rectTransform, float x)
        {
            Vector2 vector = rectTransform.anchoredPosition;
            vector.x = x;
            rectTransform.anchoredPosition = vector;
            return rectTransform;
        }

        /// <summary>
        /// 设置RectTransform锚定位置的Y分量
        /// </summary>
        /// <param name="rectTransform">要修改的RectTransform对象</param>
        /// <param name="y">新的Y锚定位置值</param>
        /// <returns>修改后的RectTransform对象（支持链式调用）</returns>
        public static RectTransform SetAnchoredPositionY(this RectTransform rectTransform, float y)
        {
            Vector2 vector = rectTransform.anchoredPosition;
            vector.y = y;
            rectTransform.anchoredPosition = vector;
            return rectTransform;
        }

        /// <summary>
        /// 设置RectTransform尺寸的宽度
        /// </summary>
        /// <param name="rectTransform">要修改的RectTransform对象</param>
        /// <param name="width">新的宽度值</param>
        /// <returns>修改后的RectTransform对象（支持链式调用）</returns>
        public static RectTransform SetSizeDeltaX(this RectTransform rectTransform, float width)
        {
            Vector2 sizeDelta = rectTransform.sizeDelta;
            sizeDelta.x = width;
            rectTransform.sizeDelta = sizeDelta;
            return rectTransform;
        }

        /// <summary>
        /// 设置RectTransform尺寸的高度
        /// </summary>
        /// <param name="rectTransform">要修改的RectTransform对象</param>
        /// <param name="height">新的高度值</param>
        /// <returns>修改后的RectTransform对象（支持链式调用）</returns>
        public static RectTransform SetSizeDeltaY(this RectTransform rectTransform, float height)
        {
            Vector2 sizeDelta = rectTransform.sizeDelta;
            sizeDelta.y = height;
            rectTransform.sizeDelta = sizeDelta;
            return rectTransform;
        }

        /// <summary>
        /// 将Transform转换为RectTransform
        /// </summary>
        /// <param name="transform">要转换的Transform对象</param>
        /// <returns>转换后的RectTransform对象</returns>
        public static RectTransform ToRectTransform(this Transform transform)
        {
            return (RectTransform)transform;
        }

        /// <summary>
        /// 重置对象位置
        /// </summary>
        /// <param name="transform">要修改的RectTransform对象</param>
        public static void Reset(this Transform transform)
        {
            transform.localPosition = Vector3.zero;
            transform.localRotation = Quaternion.identity;
            transform.localScale = Vector3.one;
        }
    }
}